<template>
  <div class="user-profile-container">
    <!-- 认证状态横幅 -->
    <div v-if="authInfo && authInfo.real_name" class="auth-banner verified">
      <div class="auth-banner-content">
        <span class="auth-icon">✓</span>
        <span class="auth-text">已实名认证</span>
        <span class="auth-detail">{{ authInfo.real_name }}</span>
        <button class="view-auth-btn" @click="goToRealNameAuth">查看详情</button>
      </div>
    </div>
    <div v-else class="auth-banner not-verified">
      <div class="auth-banner-content">
        <span class="auth-icon">⚠</span>
        <span class="auth-text">未实名认证</span>
        <button class="view-auth-btn" @click="goToRealNameAuth">立即认证</button>
      </div>
    </div>

    <div class="profile-header">
      <div class="avatar-section">
        <div class="avatar-wrapper">
          <img :src="userInfo.avatar || '/default-avatar.png'" :alt="userInfo.nickname" class="avatar" />
          <div v-if="authInfo && authInfo.real_name" class="verified-badge">✓</div>
        </div>
        <button class="edit-avatar-btn">✏️ 编辑</button>
      </div>
      
      <div class="user-info">
        <div class="name-row">
          <h2>{{ userInfo.nickname || '未设置昵称' }}</h2>
          <span v-if="authInfo && authInfo.real_name" class="verified-tag">已认证</span>
        </div>
        <p class="username">@{{ userInfo.username }}</p>
        <div class="user-meta">
          <span v-if="userInfo.mobile">📱 {{ formatMobile(userInfo.mobile) }}</span>
          <span v-if="userInfo.email">✉️ {{ userInfo.email }}</span>
        </div>
      </div>

      <button class="logout-btn" @click="handleLogout">退出登录</button>
    </div>

    <div class="profile-content">
      <div class="info-card">
        <h3>个人信息</h3>
        <div class="info-list">
          <div class="info-item">
            <span class="label">用户ID</span>
            <span class="value">{{ userInfo.id }}</span>
          </div>
          <div class="info-item">
            <span class="label">钉钉ID</span>
            <span class="value">{{ userInfo.dingtalk_id || '未绑定' }}</span>
          </div>
          <div class="info-item">
            <span class="label">注册时间</span>
            <span class="value">{{ formatDate(userInfo.created_at) }}</span>
          </div>
        </div>
      </div>

      <div class="info-card">
        <h3>数据统计</h3>
        <div class="stats-grid">
          <div class="stat-item">
            <div class="stat-number">0</div>
            <div class="stat-label">发布笔记</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">0</div>
            <div class="stat-label">获得点赞</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">0</div>
            <div class="stat-label">关注</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">0</div>
            <div class="stat-label">粉丝</div>
          </div>
        </div>
      </div>

      <div class="info-card">
        <h3>账户安全</h3>
        <div class="action-list">
          <div class="action-item" @click="goToRealNameAuth">
            <span class="action-icon">🆔</span>
            <span class="action-label">实名认证</span>
            <span class="action-status" :class="{ 'verified': authInfo && authInfo.is_verified, 'not-verified': !authInfo }">
              {{ authInfo && authInfo.real_name ? '已认证' : '未认证' }}
            </span>
            <span class="action-arrow">→</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部导航栏 -->
    <BottomNavigation />
  </div>
</template>

<script setup>
import { ref, onMounted, onActivated } from 'vue'
import { useRouter } from 'vue-router'
import BottomNavigation from './BottomNavigation.vue'

const router = useRouter()
const userInfo = ref({
  id: null,
  username: '',
  nickname: '',
  mobile: '',
  email: '',
  avatar: '',
  dingtalk_id: '',
  created_at: ''
})
const authInfo = ref(null)

const fetchUserInfo = async () => {
  const token = localStorage.getItem('access_token')
  
  if (!token) {
    router.push('/login')
    return
  }

  try {
    const response = await fetch('http://localhost:8000/api/user/info', {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })

    if (response.ok) {
      const data = await response.json()
      userInfo.value = data
    } else {
      localStorage.removeItem('access_token')
      router.push('/login')
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
    router.push('/login')
  }
}

const handleLogout = () => {
  localStorage.removeItem('access_token')
  router.push('/login')
}

const formatMobile = (mobile) => {
  if (!mobile) return ''
  return mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}

const formatDate = (dateStr) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN')
}

const goToRealNameAuth = () => {
  router.push('/real-name-auth')
}

const fetchAuthInfo = async () => {
  const token = localStorage.getItem('access_token')
  if (!token) return

  try {
    const response = await fetch('http://localhost:8000/api/real-name-auth/info', {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })

    if (response.ok) {
      const data = await response.json()
      if (data.success && data.auth_info) {
        authInfo.value = data.auth_info
      }
    }
  } catch (err) {
    console.error('获取认证信息失败:', err)
  }
}

onMounted(() => {
  fetchUserInfo()
  fetchAuthInfo()
})

// 页面激活时（从其他页面返回时）刷新认证信息
onActivated(() => {
  fetchAuthInfo()
})
</script>

<style scoped>
/* 全屏布局 - 个人信息页面 */
.user-profile-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 24px 24px 80px 24px;
  background: #f8f8f8;
  overflow-y: auto;
  box-sizing: border-box;
}

/* 认证状态横幅 */
.auth-banner {
  margin-bottom: 16px;
  border-radius: 16px;
  padding: 16px 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  animation: slideDown 0.3s ease;
}

.auth-banner.verified {
  background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
  border: 2px solid #28a745;
}

.auth-banner.not-verified {
  background: linear-gradient(135deg, #fff3cd 0%, #ffeeba 100%);
  border: 2px solid #ffc107;
}

.auth-banner-content {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.auth-icon {
  font-size: 24px;
  font-weight: bold;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: white;
}

.auth-banner.verified .auth-icon {
  color: #28a745;
}

.auth-banner.not-verified .auth-icon {
  color: #ffc107;
}

.auth-text {
  font-size: 18px;
  font-weight: 600;
  flex: 1;
}

.auth-banner.verified .auth-text {
  color: #155724;
}

.auth-banner.not-verified .auth-text {
  color: #856404;
}

.auth-detail {
  font-size: 16px;
  color: #155724;
  font-weight: 500;
}

.view-auth-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.auth-banner.verified .view-auth-btn {
  background: #28a745;
  color: white;
}

.auth-banner.verified .view-auth-btn:hover {
  background: #218838;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(40, 167, 69, 0.3);
}

.auth-banner.not-verified .view-auth-btn {
  background: #ffc107;
  color: #856404;
}

.auth-banner.not-verified .view-auth-btn:hover {
  background: #e0a800;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(255, 193, 7, 0.3);
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.profile-header {
  background: white;
  border-radius: 16px;
  padding: 32px;
  display: flex;
  align-items: center;
  gap: 24px;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.avatar-section {
  position: relative;
}

.avatar-wrapper {
  position: relative;
  display: inline-block;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #ff2442;
}

.verified-badge {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 28px;
  height: 28px;
  background: #28a745;
  border: 3px solid white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
  font-weight: bold;
  box-shadow: 0 2px 8px rgba(40, 167, 69, 0.4);
}

.edit-avatar-btn {
  position: absolute;
  bottom: 0;
  right: 0;
  background: #ff2442;
  color: white;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 14px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.user-info {
  flex: 1;
}

.name-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.user-info h2 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.verified-tag {
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
  color: white;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 2px 6px rgba(40, 167, 69, 0.3);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    box-shadow: 0 2px 6px rgba(40, 167, 69, 0.3);
  }
  50% {
    box-shadow: 0 2px 12px rgba(40, 167, 69, 0.5);
  }
}

.username {
  color: #999;
  font-size: 14px;
  margin: 4px 0;
}

.user-meta {
  display: flex;
  gap: 16px;
  margin-top: 8px;
  font-size: 14px;
  color: #666;
}

.logout-btn {
  padding: 10px 24px;
  background: #ff2442;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: #e01d37;
}

.profile-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}

.info-card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.info-card h3 {
  margin: 0 0 20px 0;
  font-size: 18px;
  color: #333;
}

.info-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-item:last-child {
  border-bottom: none;
}

.info-item .label {
  color: #999;
  font-size: 14px;
}

.info-item .value {
  color: #333;
  font-size: 14px;
  font-weight: 500;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.stat-item {
  text-align: center;
  padding: 16px;
  background: #fafafa;
  border-radius: 12px;
}

.stat-number {
  font-size: 24px;
  font-weight: 700;
  color: #ff2442;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 13px;
  color: #999;
}

.action-list {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.action-item {
  display: flex;
  align-items: center;
  padding: 16px 0;
  cursor: pointer;
  transition: background 0.3s ease;
  border-bottom: 1px solid #f0f0f0;
}

.action-item:last-child {
  border-bottom: none;
}

.action-item:hover {
  background: #fafafa;
  border-radius: 8px;
  padding-left: 8px;
  padding-right: 8px;
}

.action-icon {
  font-size: 20px;
  margin-right: 12px;
}

.action-label {
  flex: 1;
  font-size: 15px;
  color: #333;
}

.action-status {
  font-size: 13px;
  padding: 4px 12px;
  border-radius: 12px;
  margin-right: 8px;
}

.action-status.verified {
  background: #d4edda;
  color: #155724;
}

.action-status.not-verified {
  background: #f8d7da;
  color: #721c24;
}

.action-arrow {
  font-size: 18px;
  color: #999;
}

/* 响应式设计 - 移动端适配 */
@media (max-width: 768px) {
  .user-profile-container {
    padding: 16px;
  }

  .auth-banner {
    padding: 12px 16px;
    margin-bottom: 12px;
  }

  .auth-banner-content {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .auth-text {
    font-size: 16px;
  }

  .auth-detail {
    font-size: 14px;
  }

  .view-auth-btn {
    width: 100%;
    padding: 10px;
  }

  .profile-content {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .profile-header {
    flex-direction: column;
    text-align: center;
    padding: 24px;
    gap: 16px;
  }

  .avatar {
    width: 64px;
    height: 64px;
  }

  .verified-badge {
    width: 24px;
    height: 24px;
    font-size: 14px;
  }

  .name-row {
    justify-content: center;
  }

  .verified-tag {
    font-size: 12px;
    padding: 3px 10px;
  }

  .info-card {
    padding: 20px;
  }
}

@media (max-width: 480px) {
  .user-profile-container {
    padding: 12px;
  }

  .profile-header {
    padding: 20px;
    margin-bottom: 16px;
  }

  .info-card {
    padding: 16px;
  }

  .stat-number {
    font-size: 20px;
  }
}

/* 确保在大屏幕上内容居中且有合理的最大宽度 */
@media (min-width: 1200px) {
  .user-profile-container {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .profile-header {
    max-width: 1200px;
    width: 100%;
  }

  .profile-content {
    max-width: 1200px;
    width: 100%;
  }
}
</style>

